package cn.tedu.tmall.front.mall.dao.repository.impl;

import cn.tedu.tamll.common.pojo.vo.PageData;
import cn.tedu.tmall.front.mall.dao.mapper.OrderItemMapper;
import cn.tedu.tmall.front.mall.dao.mapper.OrderMapper;
import cn.tedu.tmall.front.mall.dao.repository.IOrderRepository;
import cn.tedu.tmall.front.mall.pojo.po.OrderItemPO;
import cn.tedu.tmall.front.mall.pojo.po.OrderPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OrderRepository implements IOrderRepository {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;


    @Override
    public Integer save(OrderPO orderPO) {
       return orderMapper.insert(orderPO);
    }

    @Override
    public PageData<OrderPO> pageOrderByUserId(Long userId, Integer pageSize, Integer pageNum) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("buyer_id",userId);
        PageHelper.startPage(pageNum,pageSize);
        List list = orderMapper.selectList(queryWrapper);
        PageInfo<OrderPO> pageInfo = new PageInfo<>(list);
        PageData<OrderPO> result = new PageData<>();
        BeanUtils.copyProperties(pageInfo,result);
        return result;
    }

    @Override
    public List<OrderItemPO> listOrderItemByOrderId(Long orderId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",orderId);
        return  orderItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<OrderItemPO> listOrderItemByOrderIds(List<Long> orderIds) {
       QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.in("order_id",orderIds);
       return orderItemMapper.selectList(queryWrapper);
    }

    @Override
    public OrderPO getOrderByUserIdAndOrderNo(Long userId, String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("buyer_id",userId);
        queryWrapper.eq("order_no",orderNo);
       return orderMapper.selectOne(queryWrapper);
    }
}
